﻿#region 头注释
/******************************************************************************
文件名称    :   History.cs
创建日期    :   2011/5/16 23:11:34
功能描述    :   History实体类
修改日志    :   
    2011-05-16 23:11  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// History实体类
    /// </summary>
    public class History : EntityBase<History, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public History()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            patient = null;
            patientId = null;
            historyFamily = null;
            historyFamilyId = null;
            historyEndocrine = null;
            historyEndocrineId = null;
            stoneSurgeryHistory = null;
            paishiHistory = null;
            medicineHistory = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override History CreateNew()
        {
            return new History();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.History; }
        }

        /// <summary>
        /// 获取或设置PatientId
        /// </summary>
        public Patient Patient
        {
            get { return patient; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != patientId) entityState = EntityState.Changed;
                patient = value;
            }
        }
        protected Patient patient;

        /// <summary>
        /// 获取或设置PatientId
        /// </summary>
        public long? PatientId
        {
            get { return patient == null ? patientId : patient.Id; }
            set { patientId = value; }
        }
        protected long? patientId;

        /// <summary>
        /// 获取或设置家族史
        /// </summary>
        public HistoryFamily HistoryFamily
        {
            get { return historyFamily; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != historyFamilyId) entityState = EntityState.Changed;
                historyFamily = value;
            }
        }
        protected HistoryFamily historyFamily;

        /// <summary>
        /// 获取或设置家族史
        /// </summary>
        public long? HistoryFamilyId
        {
            get { return historyFamily == null ? historyFamilyId : historyFamily.Id; }
            set { historyFamilyId = value; }
        }
        protected long? historyFamilyId;

        /// <summary>
        /// 获取或设置内分泌史
        /// </summary>
        public HistoryEndocrine HistoryEndocrine
        {
            get { return historyEndocrine; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != historyEndocrineId) entityState = EntityState.Changed;
                historyEndocrine = value;
            }
        }
        protected HistoryEndocrine historyEndocrine;

        /// <summary>
        /// 获取或设置内分泌史
        /// </summary>
        public long? HistoryEndocrineId
        {
            get { return historyEndocrine == null ? historyEndocrineId : historyEndocrine.Id; }
            set { historyEndocrineId = value; }
        }
        protected long? historyEndocrineId;

        /// <summary>
        /// 获取或设置结石手术史
        /// </summary>
        public bool? StoneSurgeryHistory
        {
            get { return stoneSurgeryHistory; }
            set
            {
                if (entityState == EntityState.Saved && stoneSurgeryHistory != value) entityState = EntityState.Changed;
                stoneSurgeryHistory = value;
            }
        }
        protected bool? stoneSurgeryHistory;

        /// <summary>
        /// 获取或设置排石史
        /// </summary>
        public bool? PaishiHistory
        {
            get { return paishiHistory; }
            set
            {
                if (entityState == EntityState.Saved && paishiHistory != value) entityState = EntityState.Changed;
                paishiHistory = value;
            }
        }
        protected bool? paishiHistory;

        /// <summary>
        /// 获取或设置服药史
        /// </summary>
        public string MedicineHistory
        {
            get { return medicineHistory; }
            set
            {
                if (entityState == EntityState.Saved && medicineHistory != value) entityState = EntityState.Changed;
                medicineHistory = value;
            }
        }
        protected string medicineHistory;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override History CopyTo(History entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.patient = this.patient;
            entity.patientId = this.patientId;
            entity.historyFamily = this.historyFamily;
            entity.historyFamilyId = this.historyFamilyId;
            entity.historyEndocrine = this.historyEndocrine;
            entity.historyEndocrineId = this.historyEndocrineId;
            entity.stoneSurgeryHistory = this.stoneSurgeryHistory;
            entity.paishiHistory = this.paishiHistory;
            entity.medicineHistory = this.medicineHistory;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<History>
        {
            History entity;
            public DefaultXmlSerializer(History entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public History Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "PatientId":
                        entity.patientId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "HistoryFamilyId":
                        entity.historyFamilyId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "HistoryEndocrineId":
                        entity.historyEndocrineId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "StoneSurgeryHistory":
                        entity.stoneSurgeryHistory = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "PaishiHistory":
                        entity.paishiHistory = XmlConvert.ToBoolean(reader.ReadElementString());
                        break;
                    case "MedicineHistory":
                        entity.medicineHistory = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.PatientId != null) writer.WriteElementString("PatientId", XmlConvert.ToString(entity.PatientId.Value));
                if (entity.HistoryFamilyId != null) writer.WriteElementString("HistoryFamilyId", XmlConvert.ToString(entity.HistoryFamilyId.Value));
                if (entity.HistoryEndocrineId != null) writer.WriteElementString("HistoryEndocrineId", XmlConvert.ToString(entity.HistoryEndocrineId.Value));
                if (entity.StoneSurgeryHistory != null) writer.WriteElementString("StoneSurgeryHistory", XmlConvert.ToString(entity.StoneSurgeryHistory.Value));
                if (entity.PaishiHistory != null) writer.WriteElementString("PaishiHistory", XmlConvert.ToString(entity.PaishiHistory.Value));
                if (entity.MedicineHistory != null) writer.WriteElementString("MedicineHistory", entity.MedicineHistory);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<History>
        {
            History entity;
            public DefaultJsonSerializer(History entity)
                : base()
            {
                this.entity = entity;
            }

            public History Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "patientId":
                        entity.patientId = DbConvert.ToNullableInt64(propValue) ?? entity.patientId;
                        break;
                    case "historyFamilyId":
                        entity.historyFamilyId = DbConvert.ToNullableInt64(propValue) ?? entity.historyFamilyId;
                        break;
                    case "historyEndocrineId":
                        entity.historyEndocrineId = DbConvert.ToNullableInt64(propValue) ?? entity.historyEndocrineId;
                        break;
                    case "stoneSurgeryHistory":
                        entity.stoneSurgeryHistory = DbConvert.ToNullableBoolean(propValue) ?? entity.stoneSurgeryHistory;
                        break;
                    case "paishiHistory":
                        entity.paishiHistory = DbConvert.ToNullableBoolean(propValue) ?? entity.paishiHistory;
                        break;
                    case "medicineHistory":
                        entity.medicineHistory = Convert.ToString(propValue) ?? entity.medicineHistory;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //PatientId
                writer.WritePropertyName("patientId");
                if (entity.PatientId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.PatientId.Value);
                }

                //家族史
                writer.WritePropertyName("historyFamilyId");
                if (entity.HistoryFamilyId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.HistoryFamilyId.Value);
                }

                //内分泌史
                writer.WritePropertyName("historyEndocrineId");
                if (entity.HistoryEndocrineId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.HistoryEndocrineId.Value);
                }

                //结石手术史
                writer.WritePropertyName("stoneSurgeryHistory");
                if (entity.StoneSurgeryHistory == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.StoneSurgeryHistory.Value);
                }

                //排石史
                writer.WritePropertyName("paishiHistory");
                if (entity.PaishiHistory == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.PaishiHistory.Value);
                }

                //服药史
                writer.WritePropertyName("medicineHistory");
                if (entity.MedicineHistory == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.MedicineHistory);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}